<?php
namespace App\Admin\Controllers\OverSeasBa;

use App\Admin\Extensions\Grid\Tool\Export;
use App\Models\Basics\SysPlatAccount;
use App\Models\OverSeasBa\BoxTranLog;
use App\Util\RExcel;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\Request;
class InventoryBoxTransferLogController extends AdminController{

    protected $title = '中转库存换标日志';

    protected function grid()
    {
        $grid = new Grid(new BoxTranLog());
        $grid->model()->orderBy('id', 'DESC');
        $grid->column('id','ID');
        $grid->column('batch_no','批次号');
        $grid->column('box_no','箱号');
        $grid->column('oaccount_id','出账号')->display(function ($value){
            return SysPlatAccount::getAccountById($value);
        });
        $grid->column('iaccount_id','入账号')->display(function ($value){
            return SysPlatAccount::getAccountById($value);
        });
        $grid->column('ofnsku','出FNSKU');
        $grid->column('ifnsku','入FNSKU');
        $grid->column('transfer_num','数量');
        $grid->column('ouser_id','操作人')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('created_at','操作时间');
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->disableExport();
        $grid->tools(function (Grid\Tools $tools){
            $tools->append(new Export('overseas-ba/box-in-trans-log-download'));
        });
        $grid->disableRowSelector();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->equal('batch_no','批次号');
                $filter->equal('ouser_id', '操作人')->select(User::getUserOptions());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('oaccount_id', '出账号')->select(SysPlatAccount::options());
                $filter->equal('iaccount_id', '入账号')->select(SysPlatAccount::options());
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('ofnsku','出FNSKU');
                $filter->equal('ifnsku','入FNSKU');
            });
        });
        return $grid;
    }

    public function download(){
        $request = Request::input();
        $query = $this->_initQuery($request);
        $this->_exeDownload($query);
    }

    private function _initQuery($filters){
        $query = BoxTranLog::query()->orderBy('id', 'DESC');
        foreach ($filters as $key => $_filter){
            if(in_array($key,['batch_no','ouser_id','oaccount_id','iaccount_id','ofnsku','ifnsku'])){
                $query->where($key,$_filter);
            }
        }
        return $query;
    }

    private function _exeDownload($query){
        $head = ['批次号','箱号','出账号','入账号','出FNSKU','入FNSKU','数量','操作人'];
        RExcel::exportCsv('中转库存换标日志',function () use ($head, $query) {
            $fp = fopen('php://output', 'w');
            fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
            fputcsv($fp, $head);
            $query->chunk(1000, function ($re) use ($fp){
                $data = self::getDownloadData($re);
                foreach ($data as $item) {
                    fputcsv($fp, $item);
                }
            });
        });
    }

    public static function getDownloadData($res)
    {
        $account = [];
        $user = [];
        foreach ($res as $model){
            $oaccount_id = $model->oaccount_id ?? -1;
            if(!array_key_exists($oaccount_id,$account)){
                $account[$oaccount_id] = SysPlatAccount::getAccountById($oaccount_id);
            }
            $iaccount_id = $model->iaccount_id ?? -1;
            if(!array_key_exists($iaccount_id,$account)){
                $account[$iaccount_id] = SysPlatAccount::getAccountById($iaccount_id);
            }
            $ouser_id = $model->ouser_id ?? -1;
            if(!array_key_exists($ouser_id,$user)){
                $user[$ouser_id] = User::getUsernameById($ouser_id);
            }
            $temp = [
                $model->batch_no,
                $model->box_no,
                $account[$oaccount_id],
                $account[$iaccount_id],
                $model->ofnsku,
                $model->ifnsku,
                $model->transfer_num,
                $user[$ouser_id],
            ];
            yield $temp;
        }
    }
}
